<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class CompanyController extends CommonController {
    //列表
    public function index(){
      $company_object = D('company');
      $map['status'] = array('egt', '0'); // 禁用和正常状态
      $Page = new Page($company_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
      $Page->setConfig('prev','«');
      $Page->setConfig('next','»');
      $data_list     = $company_object
          ->where($map)
          ->order('id asc')
          ->limit($Page->firstRow.','.$Page->listRows)
          ->select();
      $this->assign('mark','module');
      $this->assign('umark','company');
      A('list')->setMetaTitle('厂商列表')
          ->setBreadCrumb(array('模块','厂商管理','友情链接'))
          ->addTopButton('add',array('title'=>'厂商添加'))
          ->addTopButton('resume')
          ->addTopButton('delete')
          ->addTableColumn('id','ID','number',50)
          ->addTableColumn('logo','logo','image',60,'false')
          ->addTableColumn('user_id','所属用户','getuser',100,'false')
          ->addTableColumn('name','名称','text',120,'false')
          ->addTableColumn('trade','行业','text',80,'false')
          ->addTableColumn('url','官网','text','auto','false')
          ->addTableColumn('linkman','联系人','text',80,'false')
          ->addTableColumn('tel','手机号码','text',100,'false')
          ->addTableColumn('status','状态','status',60)
          ->addTableColumn('ctime','创建时间','date',120)
          ->addTableColumn('','操作','rightbtn',200,'false')
          ->setTableDataList($data_list)
          ->setTableDataPage($Page->show())
          ->addRightButton('edit')
          ->addRightButton('resume',array('title'=>'<i class="icon icon-check"></i> 通过'))
          ->addRightButton('delete')
          ->fetch();
    }
    //添加
    public function add(){
      $company_object = D('company');
      if (IS_POST) {
          $data = $company_object->create();
          if ($data) {
              $id = $company_object->add($data);
              if ($id) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'厂商新增成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'厂商新增失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$company_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','module');
        $this->assign('umark','company');
        $userdata = array();
        $udata = M('user')->field('id,nickname')->where(array('status'=>1))->select();
        foreach ($udata as $key => $value) {
            $user['value'] = $value['id'];
            $user['title'] = $value['nickname'].'（ID:'.$value['id'].'）';
            array_push($userdata,$user);
        }
        A('form')->setMetaTitle('厂商添加')
        ->setBreadCrumb(array('模块','模块管理','厂商列表','厂商添加'))
        ->setPostUrl(U('add'))
        ->addFormItem('logo','image','厂商logo')
        ->addFormItem('user_id','select','所属用户','',$userdata)
        ->addFormItem('name','text','厂商名称','',array(),array('required'=>true))
        ->addFormItem('introduce','textarea','厂商介绍')
        ->addFormItem('url','text','官方主页')
        ->addFormItem('domain','textarea','旗下域名','请输入您的网站（支持多个主域）')
        ->addFormItem('trade','text','所属行业')
        ->addFormItem('linkman','text','联系人')
        ->addFormItem('tel','text','手机号码')
        ->addFormItem('section','text','所在部门')
        ->addFormItem('email','text','企业邮箱')
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData(array('status'=>0))
        ->fetch();
      }
    }
    //编辑
    public function edit(){
      $company_object = D('company');
      if (IS_POST) {
          $data = $company_object->create();
          if ($data) {
              $rs = $company_object->save($data);
              if ($rs) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'厂商编辑成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'厂商编辑失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$company_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','module');
        $this->assign('umark','company');

        $id = I('get.ids',0,'intval');
        $form_data = $company_object->find($id);
        $userdata = array();
        $udata = M('user')->field('id,nickname')->where(array('status'=>1))->select();
        foreach ($udata as $key => $value) {
            $user['value'] = $value['id'];
            $user['title'] = $value['nickname'].'（ID:'.$value['id'].'）';
            array_push($userdata,$user);
        }
        A('form')->setMetaTitle('厂商编辑')
        ->setBreadCrumb(array('模块','模块管理','厂商列表','厂商编辑'))
        ->setPostUrl(U('edit'))
        ->addFormItem('id','hidden')
        ->addFormItem('logo','image','厂商logo')
        ->addFormItem('user_id','select','所属用户','',$userdata)
        ->addFormItem('name','text','厂商名称','',array(),array('required'=>true))
        ->addFormItem('introduce','textarea','厂商介绍')
        ->addFormItem('url','text','官方主页')
        ->addFormItem('domain','textarea','旗下域名','请输入您的网站（支持多个主域）')
        ->addFormItem('trade','text','所属行业')
        ->addFormItem('linkman','text','联系人')
        ->addFormItem('tel','text','手机号码')
        ->addFormItem('section','text','所在部门')
        ->addFormItem('email','text','企业邮箱')
        ->addFormItem('status','switch','状态','',array('启用','禁用'))
        ->setFormData($form_data)
        ->fetch();
      }
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
          $ids = I('get.ids');
          $status = I('get.status');
          $company_object = M('company');
          $map = array(
            'id' => array('in',$ids),
          );
          $save = array(
            'status' => $status,
          );
          $rs = $company_object->where($map)->save($save);
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'链接状态修改成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'链接状态修改失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $company_object = M('company');
          $map = array(
            'id' => array('in',$ids),
          );
          $rs = $company_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'链接删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'链接删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
}
